//
//  ViewController.swift
//  Swift - 01
//
//  Created by xuPeng on 2017/8/2.
//  Copyright © 2017年 陈旭鹏. All rights reserved.
//

import UIKit

/// 屏幕高度
let kScreenHeight = UIScreen.main.bounds.size.height;
/// 屏幕宽度
let kScreenWidth = UIScreen.main.bounds.size.width;

class ViewController: UIViewController {

    let timeLabel = UILabel(frame:CGRect(x:0, y:0, width:kScreenWidth, height:150))//简化创建方式
    let resetBtn = UIButton(frame:CGRect(x:kScreenWidth-80, y:20, width:80, height:50))//简化创建方式
    let playBtn = UIButton(frame:CGRect(x:0, y:150, width:kScreenWidth/2, height:kScreenHeight-150))//简化创建方式
    let pauseBtn:UIButton = UIButton(frame:CGRect(x:375/2, y:150, width:kScreenWidth/2, height:kScreenHeight-150))//简化创建方式
    var Counter = 0.0
    var Timer = Foundation.Timer()
    var IsPlaying = false
   

    override func viewDidLoad() {
        timeLabel.text = String(Counter)
        super.viewDidLoad()
        self.setUI()
    }
    
    func setUI()
    {
        
        timeLabel.backgroundColor = UIColor.lightGray
        resetBtn .setTitle("reset", for: .normal)
        resetBtn.setTitleColor(UIColor.black, for: .normal)
        playBtn.backgroundColor = UIColor.orange
        pauseBtn.backgroundColor = UIColor.purple
        
        
        timeLabel.text = String(format: "%.1f", Counter)
        timeLabel.textAlignment = .center
        timeLabel.font = UIFont(name:"Zapfino", size:20)
        
        resetBtn .addTarget(self, action: #selector(resetButtonDidTouch(sender:)), for: .touchUpInside)
        playBtn .addTarget(self, action: #selector(playButtonDidTouch(sender:)), for: .touchUpInside)
        pauseBtn .addTarget(self, action: #selector(pauseButtonDidTouch(sender:)), for: .touchUpInside)
        
        self.view.addSubview(timeLabel)
        self.view.addSubview(resetBtn)
        self.view.addSubview(playBtn)
        self.view.addSubview(pauseBtn)
       
    }

    func resetButtonDidTouch(sender: AnyObject) {
        Timer.invalidate()
        IsPlaying = false
        Counter = 0
        timeLabel.text = String(Counter)
        playBtn.isEnabled = true
        pauseBtn.isEnabled = true
    }

    
    func playButtonDidTouch(sender: AnyObject) {
        if(IsPlaying) {
            return
        }
        playBtn.isEnabled = false
        pauseBtn.isEnabled = true
        Timer = Foundation.Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.UpdateTimer), userInfo: nil, repeats: true)
        IsPlaying = true
    }

    func pauseButtonDidTouch(sender: AnyObject) {
        
        playBtn.isEnabled = true
        pauseBtn.isEnabled = false
        Timer.invalidate()
        IsPlaying = false
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func UpdateTimer() {
        Counter = Counter + 0.1
        timeLabel.text = String(format: "%.1f", Counter)
    }
}

